﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
using UpLoadDataOnTime.DataBaseUtil;
using UpLoadDataOnTime.Entity;
using UpLoadDataOnTime.Model;
using System.Reflection;
using log4net;

namespace UpLoadDataOnTime
{
    public partial class CheckInterfaceForm : Form
    {
        private GetMZData GetMZData;
        private GetZYData GetZYData;
        private int flag = 0;
        ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public CheckInterfaceForm()
        {
            InitializeComponent();
            GetMZData = new GetMZData();
            GetZYData = new GetZYData();
        }

        private void WebPost(string RequestMethodName, string json, string LogStr)
        {
            //上传数据
            string url = ConfigurationManager.ConnectionStrings["url"].ConnectionString;
            RestClient client = new RestClient(url);
            //参数1为服务名称
            RestRequest request = new RestRequest(RequestMethodName, Method.POST);
            request.AddHeader("accessAccount", ConfigurationManager.AppSettings["accessAccount"]);
            request.AddHeader("sig", ConfigurationManager.AppSettings["sig"]);
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            //执行请求并返回结果
            IRestResponse response = client.Execute(request);
            if (response.IsSuccessful)
            {
                InputRichTextBox.AppendText(" ->" + response.Content + "\n");
                Response res = JsonConvert.DeserializeObject<Response>(response.Content);
                //写入一条新log
                if (res.code.Equals("0"))
                {
                    log.Info(DateTime.Now.ToString() + RequestMethodName + " " + res.msg + LogStr);
                }
                else
                {
                    log.Warn(DateTime.Now.ToString() + RequestMethodName + " " + res.msg + LogStr);
                }

            }
            else
            {
                InputRichTextBox.AppendText(" ->" + RequestMethodName + "发送请求失败" + "\n");
                log.Error(DateTime.Now.ToString() + RequestMethodName + "发送请求失败");
            }
        }

        private void ShowProgress(string str)
        {
            //让文本框获取焦点 
            this.InputRichTextBox.Focus();
            //设置光标的位置到文本尾
            this.InputRichTextBox.Select(this.InputRichTextBox.TextLength, 0);
            //滚动到控件光标处
            this.InputRichTextBox.ScrollToCaret();
            InputRichTextBox.AppendText(str);
            progressBar1.Value++;
            Application.DoEvents();
        }

        //门诊患者基本信息
        private void MZPatientInfoButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            InputRichTextBox.Clear();
            //获取数据(请求使用了RestSharp库)
            string beginDate = BeignDateTimePicker.Value.ToString("yyyy-MM-dd");
            string endDate = EndDateTimePicker.Value.ToString("yyyy-MM-dd");
            List<MZPatientInfo> list = GetMZData.getMZPatientInfo(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            progressBar1.Minimum = 0;
            try
            {
                progressBar1.Maximum = list.Count;

                foreach (MZPatientInfo patient in list)
                {
                    if (flag == 1)
                    {
                        break;
                    }
                    string temp = "门诊患者基本信息上传: ID号:" + patient.grbsh + " 姓名:" + patient.xm;
                    //上传数据
                    WebPost("saveMZTbPatientInfo", JsonConvert.SerializeObject(patient), temp);
                    ShowProgress(temp);
                }
            }
            catch (NullReferenceException exception)
            {
                MessageBox.Show("未查询出数据");
                log.Error(exception);
            }
        }

        //门诊挂号信息
        private void MZRegistInfoButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            InputRichTextBox.Clear();
            List<MZTbResistration> list = GetMZData.GetMZTbResistrations(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbResistration mz in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊挂号信息上传: ID号:" + mz.grbsh + " 时间:" + mz.gthsj;
                WebPost("saveMZTbResistration", JsonConvert.SerializeObject(mz), temp);
                ShowProgress(temp);
            }
        }

        //门诊就诊信息
        private void MZTreatButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            InputRichTextBox.Clear();
            List<MZTbClinicRecord> list = GetMZData.GetMZTbClinicRecords(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbClinicRecord record in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊就诊信息上传: " + "ID号:" + record.grbsh + "日期:" + record.jzksrq;
                WebPost("saveMZTbClinicRecord", JsonConvert.SerializeObject(record), temp);
                ShowProgress(temp);
            }
        }

        //门诊处方信息
        private void MZPrescriptionButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<MZTbRecipe> list = GetMZData.GetMZTbRecipes(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbRecipe recipe in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊处方信息上传: " + "ID号:" + recipe.grbsh + "门诊号:" + recipe.jzlsh;
                ShowProgress(temp);
                WebPost("saveMZTbRecipe", JsonConvert.SerializeObject(recipe), temp);
            }
        }

        //门诊处方药品信息
        private void MZDrugInfoButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<MZTbMedicine> list = GetMZData.GetMZTbMedicines(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbMedicine medici in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊处方药品信息上传： ID号:" + medici.grbsh + " 处方主ID:" + medici.cfzid;
                ShowProgress(temp);
                WebPost("saveMZTbMedicine", JsonConvert.SerializeObject(medici), temp);
            }


        }

        //门诊手术信息
        private void MZOperationButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<MZTbOperation> list = GetMZData.GetMZTbOperations(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbOperation operation in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊手术信息上传: ID号:" + operation.grbsh + " 就诊流水号:" + operation.jzlsh;
                ShowProgress(temp);
                WebPost("saveMZTbOperation", JsonConvert.SerializeObject(operation), temp);
            }


        }

        //门诊检查信息
        private void MZCheckButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<MZTbCheckRecord> list = GetMZData.GetMZTbCheckRecords(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbCheckRecord check in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊检查信息: ID号:" + check.grbsh + " 就诊流水号:" + check.jzlsh;
                ShowProgress(temp);
                WebPost("saveMZTbCheckRecord", JsonConvert.SerializeObject(check), temp);
            }
        }

        //门诊检验信息
        private void MZLibButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<MZTbAssayRecord> list = GetMZData.GetMZTbAssayRecords(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbAssayRecord record in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊检验信息: ID号:" + record.grbsh + " 就诊流水号:" + record.jzlsh;
                ShowProgress(temp);
                WebPost("saveMZTbAssayRecord", JsonConvert.SerializeObject(record), temp);
            }
        }

        //门诊检验结果指标信息
        private void MZResultButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<MZTbAssayQuotaResult> list = GetMZData.GetMZTbAssayQuotaResults(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (MZTbAssayQuotaResult result in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "门诊检验结果指标信息: ID号" + result.jybgid + " 日期:" + result.bgrq;
                ShowProgress(temp);
                WebPost("saveMZTbAssayQuotaResult", JsonConvert.SerializeObject(result), temp);
            }
        }

        //住院患者基本信息
        private void ZYInfoButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbPatientInfo> list = GetZYData.GetZYTbPatientInfos(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbPatientInfo info in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "ID号:" + info.grbsh + " 姓名:" + info.xm;
                ShowProgress(temp);
                WebPost("saveZYTbPatientInfo", JsonConvert.SerializeObject(info), temp);
            }
        }

        //住院记录信息
        private void ZYTbHospitalizationRecordButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbHospitalizationRecord> list = GetZYData.GetZYTbHospitalizationRecords(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbHospitalizationRecord record in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "ID号:" + record.grbsh + " 住院号:" + record.jzlsh;
                ShowProgress(temp);
                WebPost("saveZYTbHospitalizationRecord", JsonConvert.SerializeObject(record), temp);
            }
        }

        //出院小结信息
        private void ZYOutPatientButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbDischargeAbstract> list = GetZYData.GetZYTbDischargeAbstracts(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbDischargeAbstract abs in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "ID号:" + abs.grbsh + " 住院号:" + abs.jzlsh;
                ShowProgress(temp);
                WebPost("saveZYTbDischargeAbstract", JsonConvert.SerializeObject(abs), temp);
            }
        }

        //住院手术信息
        private void ZYOperationButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbOperation> list = GetZYData.GetZYTbOperations(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbOperation operation in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "ID号:" + operation.grbsh + " 住院号:" + operation.jzlsh;
                ShowProgress(temp);
                WebPost("saveZYTbOperation", JsonConvert.SerializeObject(operation), temp);
            }
        }

        //住院检查信息
        private void ZYCheckButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbCheckRecord> list = GetZYData.GetZYTbCheckRecords(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbCheckRecord record in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "ID号:" + record.grbsh + " 住院号:" + record.jzlsh;
                ShowProgress(temp);
                WebPost("saveZYTbCheckRecord", JsonConvert.SerializeObject(record), temp);
            }
        }

        //住院检验信息
        private void ZYLibButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbAssayRecord> list = GetZYData.GetZYTbAssayRecords(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbAssayRecord record in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "ID号:" + record.grbsh + " 住院号:" + record.jzlsh;
                ShowProgress(temp);
                WebPost("saveZYTbAssayRecord", JsonConvert.SerializeObject(record), temp);
            }
            Console.Write("dxper.net".Substring(3).Replace(".", "").ToUpper());
        }

        //住院检验结果指标信息
        private void ZYResultButton_Click(object sender, EventArgs e)
        {
            flag = 0;
            progressBar1.Value = 0;
            List<ZYTbAssayQuotaResult> list = GetZYData.GetZYTbAssayQuotaResults(BeignDateTimePicker.Value.ToString("yyyy-MM-dd"), EndDateTimePicker.Value.ToString("yyyy-MM-dd"));
            InputRichTextBox.Clear();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = list.Count;
            foreach (ZYTbAssayQuotaResult result in list)
            {
                if (flag == 1)
                {
                    break;
                }
                string temp = "检验报告ID:" + result.jybgid + " 日期:" + result.bgrq;
                ShowProgress(temp);
                WebPost("saveZYTbAssayQuotaResult", JsonConvert.SerializeObject(result), temp);
            }
        }
            
        //停止测试
        private void StopButton_Click(object sender, EventArgs e)
        {
            flag = 1;
            InputRichTextBox.AppendText("\n 停止测试.....................");
        }
    }
}
